package lib;

import java.util.ArrayList;

public class Statistics {
	public static double expectedValue(double[] valueArray)
	{
		double sum=0.0;
		double average=0.0;
		for(int i=0;i<valueArray.length;i++)
		{
  			sum=sum+valueArray[i];
		}
		average=sum/valueArray.length;
		return average;
	}
	public static double Coriance(double[] valueArray)
	{
		double cori=0.0;
		double average=expectedValue(valueArray);
		for(int i=0;i<valueArray.length;i++)
		{
			cori=cori+(valueArray[i]-average)*(valueArray[i]-average);
		}
		return cori/valueArray.length;
	}
	public static double expectedValue(ArrayList<Double> valueList)
	{
		double sum=0.0;
		double average=0.0;
		for(double iter_value:valueList)
		{
  			sum=sum+iter_value;
		}
		average=sum/valueList.size();
		return average;
	}
	public static double Coriance(ArrayList<Double> valueList)
	{
		double cori=0.0;
		double average=expectedValue(valueList);
		for(double iter_value:valueList)
		{
			cori=cori+(iter_value-average)*(iter_value-average);
		}
		return cori/valueList.size();
	}
	public static double Covariance(double[] valueArray_a,double[] valueArray_b)
	{
		double cova=0.0;
		double average_a=expectedValue(valueArray_a);
		double average_b=expectedValue(valueArray_b);
		
		for(int i=0;i<valueArray_a.length;i++)
		{
			cova=cova+(valueArray_a[i]-average_a)*(valueArray_b[i]-average_b);
		}
		return cova/valueArray_a.length;
	}
	public static double Correlation(double[] valueArray_a,double[] valueArray_b)
	{
		double coriance_a=Coriance(valueArray_a);
		double coriance_b=Coriance(valueArray_b);
		double covariation=Covariance(valueArray_a,valueArray_b);
		double correlation=covariation/Math.sqrt(coriance_a*coriance_b);
		return correlation;
		
	}

}
